{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Zipline Backtest with Single Factor "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Please use with `conda` environment `ml4t-zipline`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T19:49:31.874394Z",
     "start_time": "2020-06-22T19:49:31.868875Z"
    }
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T19:49:32.678937Z",
     "start_time": "2020-06-22T19:49:31.876209Z"
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sns.set_style('whitegrid')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are first going to illustrate the zipline alpha factor research workflow in an offline environment. In particular, we will develop and test a simple mean-reversion factor that measures how much recent performance has deviated from the historical average. \n",
    "\n",
    "Short-term reversal is a common strategy that takes advantage of the weakly predictive pattern that stock price increases are likely to mean-revert back down over horizons from less than a minute to one month."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To this end, the factor computes the z-score for the last monthly return relative to the rolling monthly returns over the last year. At this point, we will not place any orders to simply illustrate the implementation of a CustomFactor and record the results during the simulation.\n",
    "\n",
    "After some basic settings, `MeanReversion` subclasses `CustomFactor` and defines a `compute()` method. It creates default inputs of monthly returns over an also default year-long window so that the monthly_return variable will have 252 rows and one column for each security in the Quandl dataset on a given day.\n",
    "\n",
    "The `compute_factors()` method creates a `MeanReversion` factor instance and creates long, short, and ranking pipeline columns. The former two contain Boolean values that could be used to place orders, and the latter reflects that overall ranking to evaluate the overall factor performance. Furthermore, it uses the built-in `AverageDollarVolume` factor to limit the computation to more liquid stocks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The result would allow us to place long and short orders. We will see in the next chapter how to build a portfolio by choosing a rebalancing period and adjusting portfolio holdings as new signals arrive."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- The `initialize()` method registers the compute_factors() pipeline, and the before_trading_start() method ensures the pipeline runs on a daily basis. \n",
    "- The `record()` function adds the pipeline's ranking column as well as the current asset prices to the performance DataFrame returned by the `run_algorithm()` function"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will use the factor and pricing data stored in the performance DataFrame to evaluate the factor performance for various holding periods in the next section, but first, we'll take a look at how to create more complex signals by combining several alpha factors from a diverse set of data sources on the Quantopian platform."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run using jupyter notebook extension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T19:49:33.367680Z",
     "start_time": "2020-06-22T19:49:32.684995Z"
    }
   },
   "outputs": [],
   "source": [
    "%load_ext zipline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-22T19:49:58.942218Z",
     "start_time": "2020-06-22T19:49:33.369272Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>algo_volatility</th>\n",
       "      <th>algorithm_period_return</th>\n",
       "      <th>alpha</th>\n",
       "      <th>benchmark_period_return</th>\n",
       "      <th>benchmark_volatility</th>\n",
       "      <th>beta</th>\n",
       "      <th>capital_used</th>\n",
       "      <th>ending_cash</th>\n",
       "      <th>ending_exposure</th>\n",
       "      <th>ending_value</th>\n",
       "      <th>...</th>\n",
       "      <th>short_exposure</th>\n",
       "      <th>short_value</th>\n",
       "      <th>shorts_count</th>\n",
       "      <th>sortino</th>\n",
       "      <th>starting_cash</th>\n",
       "      <th>starting_exposure</th>\n",
       "      <th>starting_value</th>\n",
       "      <th>trading_days</th>\n",
       "      <th>transactions</th>\n",
       "      <th>treasury_period_return</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-02 21:00:00+00:00</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.000340</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+07</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.000000e+07</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>1</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-05 21:00:00+00:00</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.018612</td>\n",
       "      <td>0.201355</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+07</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.000000e+07</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>2</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06 21:00:00+00:00</th>\n",
       "      <td>0.003482</td>\n",
       "      <td>-0.000380</td>\n",
       "      <td>-0.032876</td>\n",
       "      <td>-0.027340</td>\n",
       "      <td>0.142430</td>\n",
       "      <td>-0.000654</td>\n",
       "      <td>-3.118062e+06</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3114262.860</td>\n",
       "      <td>3114262.860</td>\n",
       "      <td>...</td>\n",
       "      <td>-1617262.705</td>\n",
       "      <td>-1617262.705</td>\n",
       "      <td>4</td>\n",
       "      <td>-9.165151</td>\n",
       "      <td>1.000000e+07</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "      <td>3</td>\n",
       "      <td>[{'price': 17.944748350023822, 'dt': 2015-01-0...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07 21:00:00+00:00</th>\n",
       "      <td>0.011564</td>\n",
       "      <td>0.000905</td>\n",
       "      <td>0.107625</td>\n",
       "      <td>-0.016028</td>\n",
       "      <td>0.201944</td>\n",
       "      <td>0.045161</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3127113.440</td>\n",
       "      <td>3127113.440</td>\n",
       "      <td>...</td>\n",
       "      <td>-1629987.410</td>\n",
       "      <td>-1629987.410</td>\n",
       "      <td>4</td>\n",
       "      <td>18.918320</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3114262.860</td>\n",
       "      <td>3114262.860</td>\n",
       "      <td>4</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08 21:00:00+00:00</th>\n",
       "      <td>0.044725</td>\n",
       "      <td>0.007277</td>\n",
       "      <td>0.420765</td>\n",
       "      <td>0.001574</td>\n",
       "      <td>0.233811</td>\n",
       "      <td>0.149003</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3190835.200</td>\n",
       "      <td>3190835.200</td>\n",
       "      <td>...</td>\n",
       "      <td>-1645106.080</td>\n",
       "      <td>-1645106.080</td>\n",
       "      <td>4</td>\n",
       "      <td>135.877107</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3127113.440</td>\n",
       "      <td>3127113.440</td>\n",
       "      <td>5</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09 21:00:00+00:00</th>\n",
       "      <td>0.052254</td>\n",
       "      <td>0.003517</td>\n",
       "      <td>0.217489</td>\n",
       "      <td>-0.006843</td>\n",
       "      <td>0.216773</td>\n",
       "      <td>0.179503</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3153233.140</td>\n",
       "      <td>3153233.140</td>\n",
       "      <td>...</td>\n",
       "      <td>-1671113.400</td>\n",
       "      <td>-1671113.400</td>\n",
       "      <td>4</td>\n",
       "      <td>6.112254</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3190835.200</td>\n",
       "      <td>3190835.200</td>\n",
       "      <td>6</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-12 21:00:00+00:00</th>\n",
       "      <td>0.049097</td>\n",
       "      <td>0.002165</td>\n",
       "      <td>0.190832</td>\n",
       "      <td>-0.014882</td>\n",
       "      <td>0.202328</td>\n",
       "      <td>0.183681</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3139710.250</td>\n",
       "      <td>3139710.250</td>\n",
       "      <td>...</td>\n",
       "      <td>-1659971.660</td>\n",
       "      <td>-1659971.660</td>\n",
       "      <td>4</td>\n",
       "      <td>3.297888</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3153233.140</td>\n",
       "      <td>3153233.140</td>\n",
       "      <td>7</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-13 21:00:00+00:00</th>\n",
       "      <td>0.046532</td>\n",
       "      <td>0.004256</td>\n",
       "      <td>0.262314</td>\n",
       "      <td>-0.017422</td>\n",
       "      <td>0.187341</td>\n",
       "      <td>0.182829</td>\n",
       "      <td>1.662399e+06</td>\n",
       "      <td>8.544337e+06</td>\n",
       "      <td>1498219.590</td>\n",
       "      <td>1498219.590</td>\n",
       "      <td>...</td>\n",
       "      <td>-1615750.780</td>\n",
       "      <td>-1615750.780</td>\n",
       "      <td>4</td>\n",
       "      <td>6.021850</td>\n",
       "      <td>6.881938e+06</td>\n",
       "      <td>3139710.250</td>\n",
       "      <td>3139710.250</td>\n",
       "      <td>8</td>\n",
       "      <td>[{'price': 23.740430510772228, 'dt': 2015-01-1...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-14 21:00:00+00:00</th>\n",
       "      <td>0.045621</td>\n",
       "      <td>0.002199</td>\n",
       "      <td>0.201495</td>\n",
       "      <td>-0.023134</td>\n",
       "      <td>0.176320</td>\n",
       "      <td>0.189158</td>\n",
       "      <td>4.253524e+04</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1435118.820</td>\n",
       "      <td>1435118.820</td>\n",
       "      <td>...</td>\n",
       "      <td>-1587033.870</td>\n",
       "      <td>-1587033.870</td>\n",
       "      <td>4</td>\n",
       "      <td>2.632596</td>\n",
       "      <td>8.544337e+06</td>\n",
       "      <td>1498219.590</td>\n",
       "      <td>1498219.590</td>\n",
       "      <td>9</td>\n",
       "      <td>[{'price': 20.82999169170143, 'dt': 2015-01-14...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-15 21:00:00+00:00</th>\n",
       "      <td>0.043015</td>\n",
       "      <td>0.002354</td>\n",
       "      <td>0.230699</td>\n",
       "      <td>-0.032168</td>\n",
       "      <td>0.169609</td>\n",
       "      <td>0.182252</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1436672.200</td>\n",
       "      <td>1436672.200</td>\n",
       "      <td>...</td>\n",
       "      <td>-1561330.410</td>\n",
       "      <td>-1561330.410</td>\n",
       "      <td>4</td>\n",
       "      <td>2.671096</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1435118.820</td>\n",
       "      <td>1435118.820</td>\n",
       "      <td>10</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-16 21:00:00+00:00</th>\n",
       "      <td>0.041510</td>\n",
       "      <td>0.001001</td>\n",
       "      <td>0.081523</td>\n",
       "      <td>-0.019175</td>\n",
       "      <td>0.179532</td>\n",
       "      <td>0.127606</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1423135.240</td>\n",
       "      <td>1423135.240</td>\n",
       "      <td>...</td>\n",
       "      <td>-1591108.560</td>\n",
       "      <td>-1591108.560</td>\n",
       "      <td>4</td>\n",
       "      <td>1.057642</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1436672.200</td>\n",
       "      <td>1436672.200</td>\n",
       "      <td>11</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-20 21:00:00+00:00</th>\n",
       "      <td>0.041285</td>\n",
       "      <td>-0.001471</td>\n",
       "      <td>0.013454</td>\n",
       "      <td>-0.017655</td>\n",
       "      <td>0.171824</td>\n",
       "      <td>0.120721</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1398419.190</td>\n",
       "      <td>1398419.190</td>\n",
       "      <td>...</td>\n",
       "      <td>-1592531.520</td>\n",
       "      <td>-1592531.520</td>\n",
       "      <td>4</td>\n",
       "      <td>-1.242293</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1423135.240</td>\n",
       "      <td>1423135.240</td>\n",
       "      <td>12</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-21 21:00:00+00:00</th>\n",
       "      <td>0.039567</td>\n",
       "      <td>-0.001994</td>\n",
       "      <td>-0.010025</td>\n",
       "      <td>-0.013007</td>\n",
       "      <td>0.166731</td>\n",
       "      <td>0.115789</td>\n",
       "      <td>1.445496e+06</td>\n",
       "      <td>1.003237e+07</td>\n",
       "      <td>-52305.640</td>\n",
       "      <td>-52305.640</td>\n",
       "      <td>...</td>\n",
       "      <td>-3528260.690</td>\n",
       "      <td>-3528260.690</td>\n",
       "      <td>9</td>\n",
       "      <td>-1.621302</td>\n",
       "      <td>8.586872e+06</td>\n",
       "      <td>1398419.190</td>\n",
       "      <td>1398419.190</td>\n",
       "      <td>13</td>\n",
       "      <td>[{'price': 10.229902194282154, 'dt': 2015-01-2...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-22 21:00:00+00:00</th>\n",
       "      <td>0.043317</td>\n",
       "      <td>-0.007029</td>\n",
       "      <td>-0.120762</td>\n",
       "      <td>0.002064</td>\n",
       "      <td>0.174355</td>\n",
       "      <td>0.050713</td>\n",
       "      <td>1.313561e+05</td>\n",
       "      <td>1.016372e+07</td>\n",
       "      <td>-234015.750</td>\n",
       "      <td>-234015.750</td>\n",
       "      <td>...</td>\n",
       "      <td>-3570182.540</td>\n",
       "      <td>-3570182.540</td>\n",
       "      <td>9</td>\n",
       "      <td>-4.053432</td>\n",
       "      <td>1.003237e+07</td>\n",
       "      <td>-52305.640</td>\n",
       "      <td>-52305.640</td>\n",
       "      <td>14</td>\n",
       "      <td>[{'price': 3.16980188119558, 'dt': 2015-01-22 ...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-23 21:00:00+00:00</th>\n",
       "      <td>0.052088</td>\n",
       "      <td>-0.015074</td>\n",
       "      <td>-0.221642</td>\n",
       "      <td>-0.003439</td>\n",
       "      <td>0.169626</td>\n",
       "      <td>0.075030</td>\n",
       "      <td>1.038210e+05</td>\n",
       "      <td>1.026754e+07</td>\n",
       "      <td>-418288.150</td>\n",
       "      <td>-418288.150</td>\n",
       "      <td>...</td>\n",
       "      <td>-3548301.140</td>\n",
       "      <td>-3548301.140</td>\n",
       "      <td>9</td>\n",
       "      <td>-5.666684</td>\n",
       "      <td>1.016372e+07</td>\n",
       "      <td>-234015.750</td>\n",
       "      <td>-234015.750</td>\n",
       "      <td>15</td>\n",
       "      <td>[{'price': 3.1498097774800997, 'dt': 2015-01-2...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-26 21:00:00+00:00</th>\n",
       "      <td>0.050726</td>\n",
       "      <td>-0.017651</td>\n",
       "      <td>-0.243601</td>\n",
       "      <td>-0.000879</td>\n",
       "      <td>0.164236</td>\n",
       "      <td>0.072121</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.026754e+07</td>\n",
       "      <td>-444053.380</td>\n",
       "      <td>-444053.380</td>\n",
       "      <td>...</td>\n",
       "      <td>-3584623.050</td>\n",
       "      <td>-3584623.050</td>\n",
       "      <td>9</td>\n",
       "      <td>-6.259988</td>\n",
       "      <td>1.026754e+07</td>\n",
       "      <td>-418288.150</td>\n",
       "      <td>-418288.150</td>\n",
       "      <td>16</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-27 21:00:00+00:00</th>\n",
       "      <td>0.052592</td>\n",
       "      <td>-0.023537</td>\n",
       "      <td>-0.282364</td>\n",
       "      <td>-0.014255</td>\n",
       "      <td>0.167160</td>\n",
       "      <td>0.099946</td>\n",
       "      <td>-2.545914e+06</td>\n",
       "      <td>7.721631e+06</td>\n",
       "      <td>2042998.040</td>\n",
       "      <td>2042998.040</td>\n",
       "      <td>...</td>\n",
       "      <td>-1426683.990</td>\n",
       "      <td>-1426683.990</td>\n",
       "      <td>4</td>\n",
       "      <td>-7.170426</td>\n",
       "      <td>1.026754e+07</td>\n",
       "      <td>-444053.380</td>\n",
       "      <td>-444053.380</td>\n",
       "      <td>17</td>\n",
       "      <td>[{'price': 43.06096537144946, 'dt': 2015-01-27...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-28 21:00:00+00:00</th>\n",
       "      <td>0.052080</td>\n",
       "      <td>-0.027626</td>\n",
       "      <td>-0.294998</td>\n",
       "      <td>-0.027558</td>\n",
       "      <td>0.168992</td>\n",
       "      <td>0.109429</td>\n",
       "      <td>1.553531e+05</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1846757.390</td>\n",
       "      <td>1846757.390</td>\n",
       "      <td>...</td>\n",
       "      <td>-1563001.600</td>\n",
       "      <td>-1563001.600</td>\n",
       "      <td>4</td>\n",
       "      <td>-7.788523</td>\n",
       "      <td>7.721631e+06</td>\n",
       "      <td>2042998.040</td>\n",
       "      <td>2042998.040</td>\n",
       "      <td>18</td>\n",
       "      <td>[{'price': 47.22978368480462, 'dt': 2015-01-28...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-29 21:00:00+00:00</th>\n",
       "      <td>0.051774</td>\n",
       "      <td>-0.032045</td>\n",
       "      <td>-0.336576</td>\n",
       "      <td>-0.018286</td>\n",
       "      <td>0.169074</td>\n",
       "      <td>0.087923</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1802570.850</td>\n",
       "      <td>1802570.850</td>\n",
       "      <td>...</td>\n",
       "      <td>-1576057.550</td>\n",
       "      <td>-1576057.550</td>\n",
       "      <td>4</td>\n",
       "      <td>-8.348722</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1846757.390</td>\n",
       "      <td>1846757.390</td>\n",
       "      <td>19</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-30 21:00:00+00:00</th>\n",
       "      <td>0.052548</td>\n",
       "      <td>-0.037758</td>\n",
       "      <td>-0.358255</td>\n",
       "      <td>-0.031041</td>\n",
       "      <td>0.170054</td>\n",
       "      <td>0.104409</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1745431.900</td>\n",
       "      <td>1745431.900</td>\n",
       "      <td>...</td>\n",
       "      <td>-1582990.520</td>\n",
       "      <td>-1582990.520</td>\n",
       "      <td>4</td>\n",
       "      <td>-8.876680</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1802570.850</td>\n",
       "      <td>1802570.850</td>\n",
       "      <td>20</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-02 21:00:00+00:00</th>\n",
       "      <td>0.058394</td>\n",
       "      <td>-0.031813</td>\n",
       "      <td>-0.299822</td>\n",
       "      <td>-0.018481</td>\n",
       "      <td>0.173176</td>\n",
       "      <td>0.142568</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1804889.610</td>\n",
       "      <td>1804889.610</td>\n",
       "      <td>...</td>\n",
       "      <td>-1573031.670</td>\n",
       "      <td>-1573031.670</td>\n",
       "      <td>4</td>\n",
       "      <td>-7.266926</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1745431.900</td>\n",
       "      <td>1745431.900</td>\n",
       "      <td>21</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-03 21:00:00+00:00</th>\n",
       "      <td>0.065715</td>\n",
       "      <td>-0.023934</td>\n",
       "      <td>-0.235887</td>\n",
       "      <td>-0.004308</td>\n",
       "      <td>0.176728</td>\n",
       "      <td>0.184529</td>\n",
       "      <td>2.086282e+06</td>\n",
       "      <td>9.963266e+06</td>\n",
       "      <td>-202605.035</td>\n",
       "      <td>-202605.035</td>\n",
       "      <td>...</td>\n",
       "      <td>-3543905.545</td>\n",
       "      <td>-3543905.545</td>\n",
       "      <td>9</td>\n",
       "      <td>-5.303852</td>\n",
       "      <td>7.876984e+06</td>\n",
       "      <td>1804889.610</td>\n",
       "      <td>1804889.610</td>\n",
       "      <td>22</td>\n",
       "      <td>[{'price': 44.57965293493293, 'dt': 2015-02-03...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-04 21:00:00+00:00</th>\n",
       "      <td>0.071302</td>\n",
       "      <td>-0.015855</td>\n",
       "      <td>-0.147289</td>\n",
       "      <td>-0.008446</td>\n",
       "      <td>0.173177</td>\n",
       "      <td>0.169684</td>\n",
       "      <td>1.617022e+05</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-283519.890</td>\n",
       "      <td>-283519.890</td>\n",
       "      <td>...</td>\n",
       "      <td>-3513473.860</td>\n",
       "      <td>-3513473.860</td>\n",
       "      <td>9</td>\n",
       "      <td>-3.400712</td>\n",
       "      <td>9.963266e+06</td>\n",
       "      <td>-202605.035</td>\n",
       "      <td>-202605.035</td>\n",
       "      <td>23</td>\n",
       "      <td>[{'price': 37.44972399418764, 'dt': 2015-02-04...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-05 21:00:00+00:00</th>\n",
       "      <td>0.070086</td>\n",
       "      <td>-0.014399</td>\n",
       "      <td>-0.144050</td>\n",
       "      <td>0.001758</td>\n",
       "      <td>0.172820</td>\n",
       "      <td>0.171044</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-268962.240</td>\n",
       "      <td>-268962.240</td>\n",
       "      <td>...</td>\n",
       "      <td>-3567682.520</td>\n",
       "      <td>-3567682.520</td>\n",
       "      <td>9</td>\n",
       "      <td>-3.016543</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-283519.890</td>\n",
       "      <td>-283519.890</td>\n",
       "      <td>24</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-06 21:00:00+00:00</th>\n",
       "      <td>0.070051</td>\n",
       "      <td>-0.010599</td>\n",
       "      <td>-0.099280</td>\n",
       "      <td>-0.001666</td>\n",
       "      <td>0.169556</td>\n",
       "      <td>0.164753</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-230962.860</td>\n",
       "      <td>-230962.860</td>\n",
       "      <td>...</td>\n",
       "      <td>-3535766.540</td>\n",
       "      <td>-3535766.540</td>\n",
       "      <td>9</td>\n",
       "      <td>-2.157376</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-268962.240</td>\n",
       "      <td>-268962.240</td>\n",
       "      <td>25</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-09 21:00:00+00:00</th>\n",
       "      <td>0.069362</td>\n",
       "      <td>-0.007829</td>\n",
       "      <td>-0.064673</td>\n",
       "      <td>-0.005906</td>\n",
       "      <td>0.166653</td>\n",
       "      <td>0.158967</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-203257.415</td>\n",
       "      <td>-203257.415</td>\n",
       "      <td>...</td>\n",
       "      <td>-3514538.275</td>\n",
       "      <td>-3514538.275</td>\n",
       "      <td>9</td>\n",
       "      <td>-1.546991</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-230962.860</td>\n",
       "      <td>-230962.860</td>\n",
       "      <td>26</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-10 21:00:00+00:00</th>\n",
       "      <td>0.068779</td>\n",
       "      <td>-0.011440</td>\n",
       "      <td>-0.106983</td>\n",
       "      <td>0.004706</td>\n",
       "      <td>0.166744</td>\n",
       "      <td>0.140496</td>\n",
       "      <td>5.258339e+05</td>\n",
       "      <td>1.065080e+07</td>\n",
       "      <td>-765199.530</td>\n",
       "      <td>-765199.530</td>\n",
       "      <td>...</td>\n",
       "      <td>-3967904.700</td>\n",
       "      <td>-3967904.700</td>\n",
       "      <td>11</td>\n",
       "      <td>-2.182483</td>\n",
       "      <td>1.012497e+07</td>\n",
       "      <td>-203257.415</td>\n",
       "      <td>-203257.415</td>\n",
       "      <td>27</td>\n",
       "      <td>[{'price': 10.930067845060593, 'dt': 2015-02-1...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-11 21:00:00+00:00</th>\n",
       "      <td>0.067499</td>\n",
       "      <td>-0.011557</td>\n",
       "      <td>-0.104292</td>\n",
       "      <td>0.004677</td>\n",
       "      <td>0.163629</td>\n",
       "      <td>0.140468</td>\n",
       "      <td>-1.529766e+04</td>\n",
       "      <td>1.063550e+07</td>\n",
       "      <td>-751078.630</td>\n",
       "      <td>-751078.630</td>\n",
       "      <td>...</td>\n",
       "      <td>-3942023.200</td>\n",
       "      <td>-3942023.200</td>\n",
       "      <td>10</td>\n",
       "      <td>-2.165752</td>\n",
       "      <td>1.065080e+07</td>\n",
       "      <td>-765199.530</td>\n",
       "      <td>-765199.530</td>\n",
       "      <td>28</td>\n",
       "      <td>[{'price': 44.720003246314675, 'dt': 2015-02-1...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-12 21:00:00+00:00</th>\n",
       "      <td>0.066440</td>\n",
       "      <td>-0.013492</td>\n",
       "      <td>-0.125340</td>\n",
       "      <td>0.014367</td>\n",
       "      <td>0.163066</td>\n",
       "      <td>0.131612</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.063550e+07</td>\n",
       "      <td>-770421.060</td>\n",
       "      <td>-770421.060</td>\n",
       "      <td>...</td>\n",
       "      <td>-3981301.150</td>\n",
       "      <td>-3981301.150</td>\n",
       "      <td>10</td>\n",
       "      <td>-2.475069</td>\n",
       "      <td>1.063550e+07</td>\n",
       "      <td>-751078.630</td>\n",
       "      <td>-751078.630</td>\n",
       "      <td>29</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-02-13 21:00:00+00:00</th>\n",
       "      <td>0.065426</td>\n",
       "      <td>-0.012482</td>\n",
       "      <td>-0.117993</td>\n",
       "      <td>0.018500</td>\n",
       "      <td>0.160556</td>\n",
       "      <td>0.132784</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.063550e+07</td>\n",
       "      <td>-760323.490</td>\n",
       "      <td>-760323.490</td>\n",
       "      <td>...</td>\n",
       "      <td>-3995741.470</td>\n",
       "      <td>-3995741.470</td>\n",
       "      <td>10</td>\n",
       "      <td>-2.246684</td>\n",
       "      <td>1.063550e+07</td>\n",
       "      <td>-770421.060</td>\n",
       "      <td>-770421.060</td>\n",
       "      <td>30</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-16 21:00:00+00:00</th>\n",
       "      <td>0.090697</td>\n",
       "      <td>0.120945</td>\n",
       "      <td>0.024013</td>\n",
       "      <td>0.255836</td>\n",
       "      <td>0.125123</td>\n",
       "      <td>0.230155</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.115831e+07</td>\n",
       "      <td>51134.340</td>\n",
       "      <td>51134.340</td>\n",
       "      <td>...</td>\n",
       "      <td>-4502894.750</td>\n",
       "      <td>-4502894.750</td>\n",
       "      <td>10</td>\n",
       "      <td>0.725267</td>\n",
       "      <td>1.115831e+07</td>\n",
       "      <td>90030.990</td>\n",
       "      <td>90030.990</td>\n",
       "      <td>726</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-17 21:00:00+00:00</th>\n",
       "      <td>0.090662</td>\n",
       "      <td>0.125350</td>\n",
       "      <td>0.025616</td>\n",
       "      <td>0.252538</td>\n",
       "      <td>0.125049</td>\n",
       "      <td>0.229863</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.115831e+07</td>\n",
       "      <td>95190.420</td>\n",
       "      <td>95190.420</td>\n",
       "      <td>...</td>\n",
       "      <td>-4490690.020</td>\n",
       "      <td>-4490690.020</td>\n",
       "      <td>10</td>\n",
       "      <td>0.747376</td>\n",
       "      <td>1.115831e+07</td>\n",
       "      <td>51134.340</td>\n",
       "      <td>51134.340</td>\n",
       "      <td>727</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-20 21:00:00+00:00</th>\n",
       "      <td>0.090688</td>\n",
       "      <td>0.117886</td>\n",
       "      <td>0.023138</td>\n",
       "      <td>0.254136</td>\n",
       "      <td>0.124965</td>\n",
       "      <td>0.229717</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.115831e+07</td>\n",
       "      <td>20545.230</td>\n",
       "      <td>20545.230</td>\n",
       "      <td>...</td>\n",
       "      <td>-4535271.190</td>\n",
       "      <td>-4535271.190</td>\n",
       "      <td>10</td>\n",
       "      <td>0.707250</td>\n",
       "      <td>1.115831e+07</td>\n",
       "      <td>95190.420</td>\n",
       "      <td>95190.420</td>\n",
       "      <td>728</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-21 21:00:00+00:00</th>\n",
       "      <td>0.090661</td>\n",
       "      <td>0.113238</td>\n",
       "      <td>0.021177</td>\n",
       "      <td>0.262339</td>\n",
       "      <td>0.124932</td>\n",
       "      <td>0.228928</td>\n",
       "      <td>-1.754553e+06</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1728621.250</td>\n",
       "      <td>1728621.250</td>\n",
       "      <td>...</td>\n",
       "      <td>-4103451.670</td>\n",
       "      <td>-4103451.670</td>\n",
       "      <td>9</td>\n",
       "      <td>0.682371</td>\n",
       "      <td>1.115831e+07</td>\n",
       "      <td>20545.230</td>\n",
       "      <td>20545.230</td>\n",
       "      <td>729</td>\n",
       "      <td>[{'price': 124.87997330466702, 'dt': 2017-11-2...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-22 21:00:00+00:00</th>\n",
       "      <td>0.090606</td>\n",
       "      <td>0.111302</td>\n",
       "      <td>0.020592</td>\n",
       "      <td>0.261392</td>\n",
       "      <td>0.124848</td>\n",
       "      <td>0.228968</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1709255.450</td>\n",
       "      <td>1709255.450</td>\n",
       "      <td>...</td>\n",
       "      <td>-4171822.000</td>\n",
       "      <td>-4171822.000</td>\n",
       "      <td>9</td>\n",
       "      <td>0.671850</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1728621.250</td>\n",
       "      <td>1728621.250</td>\n",
       "      <td>730</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-24 18:00:00+00:00</th>\n",
       "      <td>0.090584</td>\n",
       "      <td>0.106385</td>\n",
       "      <td>0.018860</td>\n",
       "      <td>0.263986</td>\n",
       "      <td>0.124766</td>\n",
       "      <td>0.228780</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1660093.180</td>\n",
       "      <td>1660093.180</td>\n",
       "      <td>...</td>\n",
       "      <td>-4205630.910</td>\n",
       "      <td>-4205630.910</td>\n",
       "      <td>9</td>\n",
       "      <td>0.645492</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1709255.450</td>\n",
       "      <td>1709255.450</td>\n",
       "      <td>731</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-27 21:00:00+00:00</th>\n",
       "      <td>0.090581</td>\n",
       "      <td>0.112730</td>\n",
       "      <td>0.020887</td>\n",
       "      <td>0.263500</td>\n",
       "      <td>0.124682</td>\n",
       "      <td>0.228686</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1723541.570</td>\n",
       "      <td>1723541.570</td>\n",
       "      <td>...</td>\n",
       "      <td>-4157384.390</td>\n",
       "      <td>-4157384.390</td>\n",
       "      <td>9</td>\n",
       "      <td>0.677795</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1660093.180</td>\n",
       "      <td>1660093.180</td>\n",
       "      <td>732</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-28 21:00:00+00:00</th>\n",
       "      <td>0.090526</td>\n",
       "      <td>0.110849</td>\n",
       "      <td>0.019554</td>\n",
       "      <td>0.275943</td>\n",
       "      <td>0.124721</td>\n",
       "      <td>0.227841</td>\n",
       "      <td>1.194458e+06</td>\n",
       "      <td>1.059822e+07</td>\n",
       "      <td>510272.200</td>\n",
       "      <td>510272.200</td>\n",
       "      <td>...</td>\n",
       "      <td>-4022066.620</td>\n",
       "      <td>-4022066.620</td>\n",
       "      <td>9</td>\n",
       "      <td>0.667596</td>\n",
       "      <td>9.403760e+06</td>\n",
       "      <td>1723541.570</td>\n",
       "      <td>1723541.570</td>\n",
       "      <td>733</td>\n",
       "      <td>[{'price': 30.779999995562946, 'dt': 2017-11-2...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-29 21:00:00+00:00</th>\n",
       "      <td>0.090560</td>\n",
       "      <td>0.118952</td>\n",
       "      <td>0.022124</td>\n",
       "      <td>0.275472</td>\n",
       "      <td>0.124637</td>\n",
       "      <td>0.227723</td>\n",
       "      <td>4.332789e+04</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>547975.480</td>\n",
       "      <td>547975.480</td>\n",
       "      <td>...</td>\n",
       "      <td>-4010804.950</td>\n",
       "      <td>-4010804.950</td>\n",
       "      <td>9</td>\n",
       "      <td>0.708729</td>\n",
       "      <td>1.059822e+07</td>\n",
       "      <td>510272.200</td>\n",
       "      <td>510272.200</td>\n",
       "      <td>734</td>\n",
       "      <td>[{'price': 46.34999201380786, 'dt': 2017-11-29...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-11-30 21:00:00+00:00</th>\n",
       "      <td>0.090509</td>\n",
       "      <td>0.116506</td>\n",
       "      <td>0.020744</td>\n",
       "      <td>0.285920</td>\n",
       "      <td>0.124636</td>\n",
       "      <td>0.227008</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>523519.640</td>\n",
       "      <td>523519.640</td>\n",
       "      <td>...</td>\n",
       "      <td>-4069421.640</td>\n",
       "      <td>-4069421.640</td>\n",
       "      <td>9</td>\n",
       "      <td>0.695637</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>547975.480</td>\n",
       "      <td>547975.480</td>\n",
       "      <td>735</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-01 21:00:00+00:00</th>\n",
       "      <td>0.090458</td>\n",
       "      <td>0.119304</td>\n",
       "      <td>0.021766</td>\n",
       "      <td>0.283316</td>\n",
       "      <td>0.124559</td>\n",
       "      <td>0.226856</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>551498.270</td>\n",
       "      <td>551498.270</td>\n",
       "      <td>...</td>\n",
       "      <td>-4032336.150</td>\n",
       "      <td>-4032336.150</td>\n",
       "      <td>9</td>\n",
       "      <td>0.709428</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>523519.640</td>\n",
       "      <td>523519.640</td>\n",
       "      <td>736</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-04 21:00:00+00:00</th>\n",
       "      <td>0.090467</td>\n",
       "      <td>0.126307</td>\n",
       "      <td>0.024027</td>\n",
       "      <td>0.281966</td>\n",
       "      <td>0.124477</td>\n",
       "      <td>0.226654</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>621526.730</td>\n",
       "      <td>621526.730</td>\n",
       "      <td>...</td>\n",
       "      <td>-3982940.390</td>\n",
       "      <td>-3982940.390</td>\n",
       "      <td>9</td>\n",
       "      <td>0.744535</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>551498.270</td>\n",
       "      <td>551498.270</td>\n",
       "      <td>737</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-05 21:00:00+00:00</th>\n",
       "      <td>0.090445</td>\n",
       "      <td>0.121360</td>\n",
       "      <td>0.022725</td>\n",
       "      <td>0.277172</td>\n",
       "      <td>0.124416</td>\n",
       "      <td>0.226984</td>\n",
       "      <td>4.180249e+06</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3608195.720</td>\n",
       "      <td>-3608195.720</td>\n",
       "      <td>...</td>\n",
       "      <td>-6304469.720</td>\n",
       "      <td>-6304469.720</td>\n",
       "      <td>14</td>\n",
       "      <td>0.718406</td>\n",
       "      <td>1.064155e+07</td>\n",
       "      <td>621526.730</td>\n",
       "      <td>621526.730</td>\n",
       "      <td>738</td>\n",
       "      <td>[{'price': 31.779983151861643, 'dt': 2017-12-0...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-06 21:00:00+00:00</th>\n",
       "      <td>0.090485</td>\n",
       "      <td>0.129801</td>\n",
       "      <td>0.025338</td>\n",
       "      <td>0.277027</td>\n",
       "      <td>0.124332</td>\n",
       "      <td>0.226906</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3523781.010</td>\n",
       "      <td>-3523781.010</td>\n",
       "      <td>...</td>\n",
       "      <td>-6246302.020</td>\n",
       "      <td>-6246302.020</td>\n",
       "      <td>14</td>\n",
       "      <td>0.760644</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3608195.720</td>\n",
       "      <td>-3608195.720</td>\n",
       "      <td>739</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-07 21:00:00+00:00</th>\n",
       "      <td>0.090447</td>\n",
       "      <td>0.126032</td>\n",
       "      <td>0.023930</td>\n",
       "      <td>0.280771</td>\n",
       "      <td>0.124257</td>\n",
       "      <td>0.226673</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3561472.250</td>\n",
       "      <td>-3561472.250</td>\n",
       "      <td>...</td>\n",
       "      <td>-6340876.140</td>\n",
       "      <td>-6340876.140</td>\n",
       "      <td>14</td>\n",
       "      <td>0.740819</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3523781.010</td>\n",
       "      <td>-3523781.010</td>\n",
       "      <td>740</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-08 21:00:00+00:00</th>\n",
       "      <td>0.090393</td>\n",
       "      <td>0.128397</td>\n",
       "      <td>0.024185</td>\n",
       "      <td>0.287824</td>\n",
       "      <td>0.124209</td>\n",
       "      <td>0.226759</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3537821.440</td>\n",
       "      <td>-3537821.440</td>\n",
       "      <td>...</td>\n",
       "      <td>-6333172.720</td>\n",
       "      <td>-6333172.720</td>\n",
       "      <td>14</td>\n",
       "      <td>0.752217</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3561472.250</td>\n",
       "      <td>-3561472.250</td>\n",
       "      <td>741</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-11 21:00:00+00:00</th>\n",
       "      <td>0.090338</td>\n",
       "      <td>0.126673</td>\n",
       "      <td>0.023383</td>\n",
       "      <td>0.291947</td>\n",
       "      <td>0.124136</td>\n",
       "      <td>0.226613</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3555067.280</td>\n",
       "      <td>-3555067.280</td>\n",
       "      <td>...</td>\n",
       "      <td>-6346412.690</td>\n",
       "      <td>-6346412.690</td>\n",
       "      <td>14</td>\n",
       "      <td>0.742976</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3537821.440</td>\n",
       "      <td>-3537821.440</td>\n",
       "      <td>742</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-12 21:00:00+00:00</th>\n",
       "      <td>0.090300</td>\n",
       "      <td>0.122956</td>\n",
       "      <td>0.022095</td>\n",
       "      <td>0.293948</td>\n",
       "      <td>0.124054</td>\n",
       "      <td>0.226516</td>\n",
       "      <td>-5.359449e+06</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1767212.130</td>\n",
       "      <td>1767212.130</td>\n",
       "      <td>...</td>\n",
       "      <td>-4059554.610</td>\n",
       "      <td>-4059554.610</td>\n",
       "      <td>9</td>\n",
       "      <td>0.723444</td>\n",
       "      <td>1.482179e+07</td>\n",
       "      <td>-3555067.280</td>\n",
       "      <td>-3555067.280</td>\n",
       "      <td>743</td>\n",
       "      <td>[{'price': 63.41015739502215, 'dt': 2017-12-12...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-13 21:00:00+00:00</th>\n",
       "      <td>0.090243</td>\n",
       "      <td>0.121452</td>\n",
       "      <td>0.021636</td>\n",
       "      <td>0.293336</td>\n",
       "      <td>0.123972</td>\n",
       "      <td>0.226541</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1752173.420</td>\n",
       "      <td>1752173.420</td>\n",
       "      <td>...</td>\n",
       "      <td>-4071517.940</td>\n",
       "      <td>-4071517.940</td>\n",
       "      <td>9</td>\n",
       "      <td>0.715331</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1767212.130</td>\n",
       "      <td>1767212.130</td>\n",
       "      <td>744</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-14 21:00:00+00:00</th>\n",
       "      <td>0.090190</td>\n",
       "      <td>0.123952</td>\n",
       "      <td>0.022725</td>\n",
       "      <td>0.288071</td>\n",
       "      <td>0.123915</td>\n",
       "      <td>0.226240</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1777172.480</td>\n",
       "      <td>1777172.480</td>\n",
       "      <td>...</td>\n",
       "      <td>-4020248.470</td>\n",
       "      <td>-4020248.470</td>\n",
       "      <td>9</td>\n",
       "      <td>0.727435</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1752173.420</td>\n",
       "      <td>1752173.420</td>\n",
       "      <td>745</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-15 21:00:00+00:00</th>\n",
       "      <td>0.090132</td>\n",
       "      <td>0.125270</td>\n",
       "      <td>0.022415</td>\n",
       "      <td>0.299631</td>\n",
       "      <td>0.123933</td>\n",
       "      <td>0.226061</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1790354.670</td>\n",
       "      <td>1790354.670</td>\n",
       "      <td>...</td>\n",
       "      <td>-4057965.550</td>\n",
       "      <td>-4057965.550</td>\n",
       "      <td>9</td>\n",
       "      <td>0.733565</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1777172.480</td>\n",
       "      <td>1777172.480</td>\n",
       "      <td>746</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-18 21:00:00+00:00</th>\n",
       "      <td>0.090071</td>\n",
       "      <td>0.125700</td>\n",
       "      <td>0.022109</td>\n",
       "      <td>0.306601</td>\n",
       "      <td>0.123884</td>\n",
       "      <td>0.225961</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1794658.140</td>\n",
       "      <td>1794658.140</td>\n",
       "      <td>...</td>\n",
       "      <td>-4091837.430</td>\n",
       "      <td>-4091837.430</td>\n",
       "      <td>9</td>\n",
       "      <td>0.735230</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1790354.670</td>\n",
       "      <td>1790354.670</td>\n",
       "      <td>747</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-19 21:00:00+00:00</th>\n",
       "      <td>0.090101</td>\n",
       "      <td>0.118067</td>\n",
       "      <td>0.019949</td>\n",
       "      <td>0.302380</td>\n",
       "      <td>0.123819</td>\n",
       "      <td>0.226449</td>\n",
       "      <td>8.140496e+05</td>\n",
       "      <td>1.027639e+07</td>\n",
       "      <td>904278.560</td>\n",
       "      <td>904278.560</td>\n",
       "      <td>...</td>\n",
       "      <td>-4530291.520</td>\n",
       "      <td>-4530291.520</td>\n",
       "      <td>11</td>\n",
       "      <td>0.695030</td>\n",
       "      <td>9.462345e+06</td>\n",
       "      <td>1794658.140</td>\n",
       "      <td>1794658.140</td>\n",
       "      <td>748</td>\n",
       "      <td>[{'price': 130.84909079974724, 'dt': 2017-12-1...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-20 21:00:00+00:00</th>\n",
       "      <td>0.090056</td>\n",
       "      <td>0.115063</td>\n",
       "      <td>0.019059</td>\n",
       "      <td>0.301302</td>\n",
       "      <td>0.123738</td>\n",
       "      <td>0.226517</td>\n",
       "      <td>7.610861e+04</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>798125.340</td>\n",
       "      <td>798125.340</td>\n",
       "      <td>...</td>\n",
       "      <td>-4534652.810</td>\n",
       "      <td>-4534652.810</td>\n",
       "      <td>10</td>\n",
       "      <td>0.679238</td>\n",
       "      <td>1.027639e+07</td>\n",
       "      <td>904278.560</td>\n",
       "      <td>904278.560</td>\n",
       "      <td>749</td>\n",
       "      <td>[{'price': 58.649540201740976, 'dt': 2017-12-2...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-21 21:00:00+00:00</th>\n",
       "      <td>0.090194</td>\n",
       "      <td>0.103773</td>\n",
       "      <td>0.015456</td>\n",
       "      <td>0.303886</td>\n",
       "      <td>0.123659</td>\n",
       "      <td>0.226142</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>685221.750</td>\n",
       "      <td>685221.750</td>\n",
       "      <td>...</td>\n",
       "      <td>-4581119.450</td>\n",
       "      <td>-4581119.450</td>\n",
       "      <td>10</td>\n",
       "      <td>0.618988</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>798125.340</td>\n",
       "      <td>798125.340</td>\n",
       "      <td>750</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-22 21:00:00+00:00</th>\n",
       "      <td>0.090139</td>\n",
       "      <td>0.102076</td>\n",
       "      <td>0.014944</td>\n",
       "      <td>0.303288</td>\n",
       "      <td>0.123577</td>\n",
       "      <td>0.226170</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>668261.720</td>\n",
       "      <td>668261.720</td>\n",
       "      <td>...</td>\n",
       "      <td>-4598653.320</td>\n",
       "      <td>-4598653.320</td>\n",
       "      <td>10</td>\n",
       "      <td>0.609931</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>685221.750</td>\n",
       "      <td>685221.750</td>\n",
       "      <td>751</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-26 21:00:00+00:00</th>\n",
       "      <td>0.090110</td>\n",
       "      <td>0.097727</td>\n",
       "      <td>0.013652</td>\n",
       "      <td>0.301909</td>\n",
       "      <td>0.123498</td>\n",
       "      <td>0.226289</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>624763.750</td>\n",
       "      <td>624763.750</td>\n",
       "      <td>...</td>\n",
       "      <td>-4592983.200</td>\n",
       "      <td>-4592983.200</td>\n",
       "      <td>10</td>\n",
       "      <td>0.587086</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>668261.720</td>\n",
       "      <td>668261.720</td>\n",
       "      <td>752</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-27 21:00:00+00:00</th>\n",
       "      <td>0.090074</td>\n",
       "      <td>0.101852</td>\n",
       "      <td>0.014846</td>\n",
       "      <td>0.302938</td>\n",
       "      <td>0.123416</td>\n",
       "      <td>0.226321</td>\n",
       "      <td>1.940099e+06</td>\n",
       "      <td>1.229260e+07</td>\n",
       "      <td>-1274086.885</td>\n",
       "      <td>-1274086.885</td>\n",
       "      <td>...</td>\n",
       "      <td>-5240774.395</td>\n",
       "      <td>-5240774.395</td>\n",
       "      <td>13</td>\n",
       "      <td>0.607626</td>\n",
       "      <td>1.035250e+07</td>\n",
       "      <td>624763.750</td>\n",
       "      <td>624763.750</td>\n",
       "      <td>753</td>\n",
       "      <td>[{'price': 134.26133731766248, 'dt': 2017-12-2...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-28 21:00:00+00:00</th>\n",
       "      <td>0.090023</td>\n",
       "      <td>0.104406</td>\n",
       "      <td>0.015466</td>\n",
       "      <td>0.305328</td>\n",
       "      <td>0.123337</td>\n",
       "      <td>0.226380</td>\n",
       "      <td>-5.744239e+04</td>\n",
       "      <td>1.223516e+07</td>\n",
       "      <td>-1191100.685</td>\n",
       "      <td>-1191100.685</td>\n",
       "      <td>...</td>\n",
       "      <td>-5158764.315</td>\n",
       "      <td>-5158764.315</td>\n",
       "      <td>12</td>\n",
       "      <td>0.620128</td>\n",
       "      <td>1.229260e+07</td>\n",
       "      <td>-1274086.885</td>\n",
       "      <td>-1274086.885</td>\n",
       "      <td>754</td>\n",
       "      <td>[{'price': 27.580005922492067, 'dt': 2017-12-2...</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-29 21:00:00+00:00</th>\n",
       "      <td>0.089995</td>\n",
       "      <td>0.109136</td>\n",
       "      <td>0.017359</td>\n",
       "      <td>0.298562</td>\n",
       "      <td>0.123297</td>\n",
       "      <td>0.225720</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.223516e+07</td>\n",
       "      <td>-1143802.510</td>\n",
       "      <td>-1143802.510</td>\n",
       "      <td>...</td>\n",
       "      <td>-5097704.930</td>\n",
       "      <td>-5097704.930</td>\n",
       "      <td>12</td>\n",
       "      <td>0.643540</td>\n",
       "      <td>1.223516e+07</td>\n",
       "      <td>-1191100.685</td>\n",
       "      <td>-1191100.685</td>\n",
       "      <td>755</td>\n",
       "      <td>[]</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>755 rows × 39 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                           algo_volatility  algorithm_period_return     alpha  benchmark_period_return  benchmark_volatility      beta  capital_used   ending_cash  ending_exposure  ending_value          ...            short_exposure  short_value  shorts_count     sortino  starting_cash  starting_exposure  starting_value  trading_days                                       transactions treasury_period_return\n",
       "2015-01-02 21:00:00+00:00              NaN                 0.000000       NaN                -0.000340                   NaN       NaN  0.000000e+00  1.000000e+07            0.000         0.000          ...                     0.000        0.000             0         NaN   1.000000e+07              0.000           0.000             1                                                 []                    0.0\n",
       "2015-01-05 21:00:00+00:00         0.000000                 0.000000  0.000000                -0.018612              0.201355  0.000000  0.000000e+00  1.000000e+07            0.000         0.000          ...                     0.000        0.000             0         NaN   1.000000e+07              0.000           0.000             2                                                 []                    0.0\n",
       "2015-01-06 21:00:00+00:00         0.003482                -0.000380 -0.032876                -0.027340              0.142430 -0.000654 -3.118062e+06  6.881938e+06      3114262.860   3114262.860          ...              -1617262.705 -1617262.705             4   -9.165151   1.000000e+07              0.000           0.000             3  [{'price': 17.944748350023822, 'dt': 2015-01-0...                    0.0\n",
       "2015-01-07 21:00:00+00:00         0.011564                 0.000905  0.107625                -0.016028              0.201944  0.045161  0.000000e+00  6.881938e+06      3127113.440   3127113.440          ...              -1629987.410 -1629987.410             4   18.918320   6.881938e+06        3114262.860     3114262.860             4                                                 []                    0.0\n",
       "2015-01-08 21:00:00+00:00         0.044725                 0.007277  0.420765                 0.001574              0.233811  0.149003  0.000000e+00  6.881938e+06      3190835.200   3190835.200          ...              -1645106.080 -1645106.080             4  135.877107   6.881938e+06        3127113.440     3127113.440             5                                                 []                    0.0\n",
       "2015-01-09 21:00:00+00:00         0.052254                 0.003517  0.217489                -0.006843              0.216773  0.179503  0.000000e+00  6.881938e+06      3153233.140   3153233.140          ...              -1671113.400 -1671113.400             4    6.112254   6.881938e+06        3190835.200     3190835.200             6                                                 []                    0.0\n",
       "2015-01-12 21:00:00+00:00         0.049097                 0.002165  0.190832                -0.014882              0.202328  0.183681  0.000000e+00  6.881938e+06      3139710.250   3139710.250          ...              -1659971.660 -1659971.660             4    3.297888   6.881938e+06        3153233.140     3153233.140             7                                                 []                    0.0\n",
       "2015-01-13 21:00:00+00:00         0.046532                 0.004256  0.262314                -0.017422              0.187341  0.182829  1.662399e+06  8.544337e+06      1498219.590   1498219.590          ...              -1615750.780 -1615750.780             4    6.021850   6.881938e+06        3139710.250     3139710.250             8  [{'price': 23.740430510772228, 'dt': 2015-01-1...                    0.0\n",
       "2015-01-14 21:00:00+00:00         0.045621                 0.002199  0.201495                -0.023134              0.176320  0.189158  4.253524e+04  8.586872e+06      1435118.820   1435118.820          ...              -1587033.870 -1587033.870             4    2.632596   8.544337e+06        1498219.590     1498219.590             9  [{'price': 20.82999169170143, 'dt': 2015-01-14...                    0.0\n",
       "2015-01-15 21:00:00+00:00         0.043015                 0.002354  0.230699                -0.032168              0.169609  0.182252  0.000000e+00  8.586872e+06      1436672.200   1436672.200          ...              -1561330.410 -1561330.410             4    2.671096   8.586872e+06        1435118.820     1435118.820            10                                                 []                    0.0\n",
       "2015-01-16 21:00:00+00:00         0.041510                 0.001001  0.081523                -0.019175              0.179532  0.127606  0.000000e+00  8.586872e+06      1423135.240   1423135.240          ...              -1591108.560 -1591108.560             4    1.057642   8.586872e+06        1436672.200     1436672.200            11                                                 []                    0.0\n",
       "2015-01-20 21:00:00+00:00         0.041285                -0.001471  0.013454                -0.017655              0.171824  0.120721  0.000000e+00  8.586872e+06      1398419.190   1398419.190          ...              -1592531.520 -1592531.520             4   -1.242293   8.586872e+06        1423135.240     1423135.240            12                                                 []                    0.0\n",
       "2015-01-21 21:00:00+00:00         0.039567                -0.001994 -0.010025                -0.013007              0.166731  0.115789  1.445496e+06  1.003237e+07       -52305.640    -52305.640          ...              -3528260.690 -3528260.690             9   -1.621302   8.586872e+06        1398419.190     1398419.190            13  [{'price': 10.229902194282154, 'dt': 2015-01-2...                    0.0\n",
       "2015-01-22 21:00:00+00:00         0.043317                -0.007029 -0.120762                 0.002064              0.174355  0.050713  1.313561e+05  1.016372e+07      -234015.750   -234015.750          ...              -3570182.540 -3570182.540             9   -4.053432   1.003237e+07         -52305.640      -52305.640            14  [{'price': 3.16980188119558, 'dt': 2015-01-22 ...                    0.0\n",
       "2015-01-23 21:00:00+00:00         0.052088                -0.015074 -0.221642                -0.003439              0.169626  0.075030  1.038210e+05  1.026754e+07      -418288.150   -418288.150          ...              -3548301.140 -3548301.140             9   -5.666684   1.016372e+07        -234015.750     -234015.750            15  [{'price': 3.1498097774800997, 'dt': 2015-01-2...                    0.0\n",
       "2015-01-26 21:00:00+00:00         0.050726                -0.017651 -0.243601                -0.000879              0.164236  0.072121  0.000000e+00  1.026754e+07      -444053.380   -444053.380          ...              -3584623.050 -3584623.050             9   -6.259988   1.026754e+07        -418288.150     -418288.150            16                                                 []                    0.0\n",
       "2015-01-27 21:00:00+00:00         0.052592                -0.023537 -0.282364                -0.014255              0.167160  0.099946 -2.545914e+06  7.721631e+06      2042998.040   2042998.040          ...              -1426683.990 -1426683.990             4   -7.170426   1.026754e+07        -444053.380     -444053.380            17  [{'price': 43.06096537144946, 'dt': 2015-01-27...                    0.0\n",
       "2015-01-28 21:00:00+00:00         0.052080                -0.027626 -0.294998                -0.027558              0.168992  0.109429  1.553531e+05  7.876984e+06      1846757.390   1846757.390          ...              -1563001.600 -1563001.600             4   -7.788523   7.721631e+06        2042998.040     2042998.040            18  [{'price': 47.22978368480462, 'dt': 2015-01-28...                    0.0\n",
       "2015-01-29 21:00:00+00:00         0.051774                -0.032045 -0.336576                -0.018286              0.169074  0.087923  0.000000e+00  7.876984e+06      1802570.850   1802570.850          ...              -1576057.550 -1576057.550             4   -8.348722   7.876984e+06        1846757.390     1846757.390            19                                                 []                    0.0\n",
       "2015-01-30 21:00:00+00:00         0.052548                -0.037758 -0.358255                -0.031041              0.170054  0.104409  0.000000e+00  7.876984e+06      1745431.900   1745431.900          ...              -1582990.520 -1582990.520             4   -8.876680   7.876984e+06        1802570.850     1802570.850            20                                                 []                    0.0\n",
       "2015-02-02 21:00:00+00:00         0.058394                -0.031813 -0.299822                -0.018481              0.173176  0.142568  0.000000e+00  7.876984e+06      1804889.610   1804889.610          ...              -1573031.670 -1573031.670             4   -7.266926   7.876984e+06        1745431.900     1745431.900            21                                                 []                    0.0\n",
       "2015-02-03 21:00:00+00:00         0.065715                -0.023934 -0.235887                -0.004308              0.176728  0.184529  2.086282e+06  9.963266e+06      -202605.035   -202605.035          ...              -3543905.545 -3543905.545             9   -5.303852   7.876984e+06        1804889.610     1804889.610            22  [{'price': 44.57965293493293, 'dt': 2015-02-03...                    0.0\n",
       "2015-02-04 21:00:00+00:00         0.071302                -0.015855 -0.147289                -0.008446              0.173177  0.169684  1.617022e+05  1.012497e+07      -283519.890   -283519.890          ...              -3513473.860 -3513473.860             9   -3.400712   9.963266e+06        -202605.035     -202605.035            23  [{'price': 37.44972399418764, 'dt': 2015-02-04...                    0.0\n",
       "2015-02-05 21:00:00+00:00         0.070086                -0.014399 -0.144050                 0.001758              0.172820  0.171044  0.000000e+00  1.012497e+07      -268962.240   -268962.240          ...              -3567682.520 -3567682.520             9   -3.016543   1.012497e+07        -283519.890     -283519.890            24                                                 []                    0.0\n",
       "2015-02-06 21:00:00+00:00         0.070051                -0.010599 -0.099280                -0.001666              0.169556  0.164753  0.000000e+00  1.012497e+07      -230962.860   -230962.860          ...              -3535766.540 -3535766.540             9   -2.157376   1.012497e+07        -268962.240     -268962.240            25                                                 []                    0.0\n",
       "2015-02-09 21:00:00+00:00         0.069362                -0.007829 -0.064673                -0.005906              0.166653  0.158967  0.000000e+00  1.012497e+07      -203257.415   -203257.415          ...              -3514538.275 -3514538.275             9   -1.546991   1.012497e+07        -230962.860     -230962.860            26                                                 []                    0.0\n",
       "2015-02-10 21:00:00+00:00         0.068779                -0.011440 -0.106983                 0.004706              0.166744  0.140496  5.258339e+05  1.065080e+07      -765199.530   -765199.530          ...              -3967904.700 -3967904.700            11   -2.182483   1.012497e+07        -203257.415     -203257.415            27  [{'price': 10.930067845060593, 'dt': 2015-02-1...                    0.0\n",
       "2015-02-11 21:00:00+00:00         0.067499                -0.011557 -0.104292                 0.004677              0.163629  0.140468 -1.529766e+04  1.063550e+07      -751078.630   -751078.630          ...              -3942023.200 -3942023.200            10   -2.165752   1.065080e+07        -765199.530     -765199.530            28  [{'price': 44.720003246314675, 'dt': 2015-02-1...                    0.0\n",
       "2015-02-12 21:00:00+00:00         0.066440                -0.013492 -0.125340                 0.014367              0.163066  0.131612  0.000000e+00  1.063550e+07      -770421.060   -770421.060          ...              -3981301.150 -3981301.150            10   -2.475069   1.063550e+07        -751078.630     -751078.630            29                                                 []                    0.0\n",
       "2015-02-13 21:00:00+00:00         0.065426                -0.012482 -0.117993                 0.018500              0.160556  0.132784  0.000000e+00  1.063550e+07      -760323.490   -760323.490          ...              -3995741.470 -3995741.470            10   -2.246684   1.063550e+07        -770421.060     -770421.060            30                                                 []                    0.0\n",
       "...                                    ...                      ...       ...                      ...                   ...       ...           ...           ...              ...           ...          ...                       ...          ...           ...         ...            ...                ...             ...           ...                                                ...                    ...\n",
       "2017-11-16 21:00:00+00:00         0.090697                 0.120945  0.024013                 0.255836              0.125123  0.230155  0.000000e+00  1.115831e+07        51134.340     51134.340          ...              -4502894.750 -4502894.750            10    0.725267   1.115831e+07          90030.990       90030.990           726                                                 []                    0.0\n",
       "2017-11-17 21:00:00+00:00         0.090662                 0.125350  0.025616                 0.252538              0.125049  0.229863  0.000000e+00  1.115831e+07        95190.420     95190.420          ...              -4490690.020 -4490690.020            10    0.747376   1.115831e+07          51134.340       51134.340           727                                                 []                    0.0\n",
       "2017-11-20 21:00:00+00:00         0.090688                 0.117886  0.023138                 0.254136              0.124965  0.229717  0.000000e+00  1.115831e+07        20545.230     20545.230          ...              -4535271.190 -4535271.190            10    0.707250   1.115831e+07          95190.420       95190.420           728                                                 []                    0.0\n",
       "2017-11-21 21:00:00+00:00         0.090661                 0.113238  0.021177                 0.262339              0.124932  0.228928 -1.754553e+06  9.403760e+06      1728621.250   1728621.250          ...              -4103451.670 -4103451.670             9    0.682371   1.115831e+07          20545.230       20545.230           729  [{'price': 124.87997330466702, 'dt': 2017-11-2...                    0.0\n",
       "2017-11-22 21:00:00+00:00         0.090606                 0.111302  0.020592                 0.261392              0.124848  0.228968  0.000000e+00  9.403760e+06      1709255.450   1709255.450          ...              -4171822.000 -4171822.000             9    0.671850   9.403760e+06        1728621.250     1728621.250           730                                                 []                    0.0\n",
       "2017-11-24 18:00:00+00:00         0.090584                 0.106385  0.018860                 0.263986              0.124766  0.228780  0.000000e+00  9.403760e+06      1660093.180   1660093.180          ...              -4205630.910 -4205630.910             9    0.645492   9.403760e+06        1709255.450     1709255.450           731                                                 []                    0.0\n",
       "2017-11-27 21:00:00+00:00         0.090581                 0.112730  0.020887                 0.263500              0.124682  0.228686  0.000000e+00  9.403760e+06      1723541.570   1723541.570          ...              -4157384.390 -4157384.390             9    0.677795   9.403760e+06        1660093.180     1660093.180           732                                                 []                    0.0\n",
       "2017-11-28 21:00:00+00:00         0.090526                 0.110849  0.019554                 0.275943              0.124721  0.227841  1.194458e+06  1.059822e+07       510272.200    510272.200          ...              -4022066.620 -4022066.620             9    0.667596   9.403760e+06        1723541.570     1723541.570           733  [{'price': 30.779999995562946, 'dt': 2017-11-2...                    0.0\n",
       "2017-11-29 21:00:00+00:00         0.090560                 0.118952  0.022124                 0.275472              0.124637  0.227723  4.332789e+04  1.064155e+07       547975.480    547975.480          ...              -4010804.950 -4010804.950             9    0.708729   1.059822e+07         510272.200      510272.200           734  [{'price': 46.34999201380786, 'dt': 2017-11-29...                    0.0\n",
       "2017-11-30 21:00:00+00:00         0.090509                 0.116506  0.020744                 0.285920              0.124636  0.227008  0.000000e+00  1.064155e+07       523519.640    523519.640          ...              -4069421.640 -4069421.640             9    0.695637   1.064155e+07         547975.480      547975.480           735                                                 []                    0.0\n",
       "2017-12-01 21:00:00+00:00         0.090458                 0.119304  0.021766                 0.283316              0.124559  0.226856  0.000000e+00  1.064155e+07       551498.270    551498.270          ...              -4032336.150 -4032336.150             9    0.709428   1.064155e+07         523519.640      523519.640           736                                                 []                    0.0\n",
       "2017-12-04 21:00:00+00:00         0.090467                 0.126307  0.024027                 0.281966              0.124477  0.226654  0.000000e+00  1.064155e+07       621526.730    621526.730          ...              -3982940.390 -3982940.390             9    0.744535   1.064155e+07         551498.270      551498.270           737                                                 []                    0.0\n",
       "2017-12-05 21:00:00+00:00         0.090445                 0.121360  0.022725                 0.277172              0.124416  0.226984  4.180249e+06  1.482179e+07     -3608195.720  -3608195.720          ...              -6304469.720 -6304469.720            14    0.718406   1.064155e+07         621526.730      621526.730           738  [{'price': 31.779983151861643, 'dt': 2017-12-0...                    0.0\n",
       "2017-12-06 21:00:00+00:00         0.090485                 0.129801  0.025338                 0.277027              0.124332  0.226906  0.000000e+00  1.482179e+07     -3523781.010  -3523781.010          ...              -6246302.020 -6246302.020            14    0.760644   1.482179e+07       -3608195.720    -3608195.720           739                                                 []                    0.0\n",
       "2017-12-07 21:00:00+00:00         0.090447                 0.126032  0.023930                 0.280771              0.124257  0.226673  0.000000e+00  1.482179e+07     -3561472.250  -3561472.250          ...              -6340876.140 -6340876.140            14    0.740819   1.482179e+07       -3523781.010    -3523781.010           740                                                 []                    0.0\n",
       "2017-12-08 21:00:00+00:00         0.090393                 0.128397  0.024185                 0.287824              0.124209  0.226759  0.000000e+00  1.482179e+07     -3537821.440  -3537821.440          ...              -6333172.720 -6333172.720            14    0.752217   1.482179e+07       -3561472.250    -3561472.250           741                                                 []                    0.0\n",
       "2017-12-11 21:00:00+00:00         0.090338                 0.126673  0.023383                 0.291947              0.124136  0.226613  0.000000e+00  1.482179e+07     -3555067.280  -3555067.280          ...              -6346412.690 -6346412.690            14    0.742976   1.482179e+07       -3537821.440    -3537821.440           742                                                 []                    0.0\n",
       "2017-12-12 21:00:00+00:00         0.090300                 0.122956  0.022095                 0.293948              0.124054  0.226516 -5.359449e+06  9.462345e+06      1767212.130   1767212.130          ...              -4059554.610 -4059554.610             9    0.723444   1.482179e+07       -3555067.280    -3555067.280           743  [{'price': 63.41015739502215, 'dt': 2017-12-12...                    0.0\n",
       "2017-12-13 21:00:00+00:00         0.090243                 0.121452  0.021636                 0.293336              0.123972  0.226541  0.000000e+00  9.462345e+06      1752173.420   1752173.420          ...              -4071517.940 -4071517.940             9    0.715331   9.462345e+06        1767212.130     1767212.130           744                                                 []                    0.0\n",
       "2017-12-14 21:00:00+00:00         0.090190                 0.123952  0.022725                 0.288071              0.123915  0.226240  0.000000e+00  9.462345e+06      1777172.480   1777172.480          ...              -4020248.470 -4020248.470             9    0.727435   9.462345e+06        1752173.420     1752173.420           745                                                 []                    0.0\n",
       "2017-12-15 21:00:00+00:00         0.090132                 0.125270  0.022415                 0.299631              0.123933  0.226061  0.000000e+00  9.462345e+06      1790354.670   1790354.670          ...              -4057965.550 -4057965.550             9    0.733565   9.462345e+06        1777172.480     1777172.480           746                                                 []                    0.0\n",
       "2017-12-18 21:00:00+00:00         0.090071                 0.125700  0.022109                 0.306601              0.123884  0.225961  0.000000e+00  9.462345e+06      1794658.140   1794658.140          ...              -4091837.430 -4091837.430             9    0.735230   9.462345e+06        1790354.670     1790354.670           747                                                 []                    0.0\n",
       "2017-12-19 21:00:00+00:00         0.090101                 0.118067  0.019949                 0.302380              0.123819  0.226449  8.140496e+05  1.027639e+07       904278.560    904278.560          ...              -4530291.520 -4530291.520            11    0.695030   9.462345e+06        1794658.140     1794658.140           748  [{'price': 130.84909079974724, 'dt': 2017-12-1...                    0.0\n",
       "2017-12-20 21:00:00+00:00         0.090056                 0.115063  0.019059                 0.301302              0.123738  0.226517  7.610861e+04  1.035250e+07       798125.340    798125.340          ...              -4534652.810 -4534652.810            10    0.679238   1.027639e+07         904278.560      904278.560           749  [{'price': 58.649540201740976, 'dt': 2017-12-2...                    0.0\n",
       "2017-12-21 21:00:00+00:00         0.090194                 0.103773  0.015456                 0.303886              0.123659  0.226142  0.000000e+00  1.035250e+07       685221.750    685221.750          ...              -4581119.450 -4581119.450            10    0.618988   1.035250e+07         798125.340      798125.340           750                                                 []                    0.0\n",
       "2017-12-22 21:00:00+00:00         0.090139                 0.102076  0.014944                 0.303288              0.123577  0.226170  0.000000e+00  1.035250e+07       668261.720    668261.720          ...              -4598653.320 -4598653.320            10    0.609931   1.035250e+07         685221.750      685221.750           751                                                 []                    0.0\n",
       "2017-12-26 21:00:00+00:00         0.090110                 0.097727  0.013652                 0.301909              0.123498  0.226289  0.000000e+00  1.035250e+07       624763.750    624763.750          ...              -4592983.200 -4592983.200            10    0.587086   1.035250e+07         668261.720      668261.720           752                                                 []                    0.0\n",
       "2017-12-27 21:00:00+00:00         0.090074                 0.101852  0.014846                 0.302938              0.123416  0.226321  1.940099e+06  1.229260e+07     -1274086.885  -1274086.885          ...              -5240774.395 -5240774.395            13    0.607626   1.035250e+07         624763.750      624763.750           753  [{'price': 134.26133731766248, 'dt': 2017-12-2...                    0.0\n",
       "2017-12-28 21:00:00+00:00         0.090023                 0.104406  0.015466                 0.305328              0.123337  0.226380 -5.744239e+04  1.223516e+07     -1191100.685  -1191100.685          ...              -5158764.315 -5158764.315            12    0.620128   1.229260e+07       -1274086.885    -1274086.885           754  [{'price': 27.580005922492067, 'dt': 2017-12-2...                    0.0\n",
       "2017-12-29 21:00:00+00:00         0.089995                 0.109136  0.017359                 0.298562              0.123297  0.225720  0.000000e+00  1.223516e+07     -1143802.510  -1143802.510          ...              -5097704.930 -5097704.930            12    0.643540   1.223516e+07       -1191100.685    -1191100.685           755                                                 []                    0.0\n",
       "\n",
       "[755 rows x 39 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%zipline --start 2015-1-1 --end 2018-1-1 --output single_factor.pickle\n",
    "\n",
    "from zipline.api import (\n",
    "    attach_pipeline,\n",
    "    date_rules,\n",
    "    time_rules,\n",
    "    order_target_percent,\n",
    "    pipeline_output,\n",
    "    record,\n",
    "    schedule_function,\n",
    "    get_open_orders,\n",
    "    calendars\n",
    ")\n",
    "from zipline.finance import commission, slippage\n",
    "from zipline.pipeline import Pipeline, CustomFactor\n",
    "from zipline.pipeline.factors import Returns, AverageDollarVolume\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "MONTH = 21\n",
    "YEAR = 12 * MONTH\n",
    "N_LONGS = N_SHORTS = 25\n",
    "VOL_SCREEN = 1000\n",
    "\n",
    "\n",
    "class MeanReversion(CustomFactor):\n",
    "    \"\"\"Compute ratio of latest monthly return to 12m average,\n",
    "       normalized by std dev of monthly returns\"\"\"\n",
    "    inputs = [Returns(window_length=MONTH)]\n",
    "    window_length = YEAR\n",
    "\n",
    "    def compute(self, today, assets, out, monthly_returns):\n",
    "        df = pd.DataFrame(monthly_returns)\n",
    "        out[:] = df.iloc[-1].sub(df.mean()).div(df.std())\n",
    "\n",
    "\n",
    "def compute_factors():\n",
    "    \"\"\"Create factor pipeline incl. mean reversion,\n",
    "        filtered by 30d Dollar Volume; capture factor ranks\"\"\"\n",
    "    mean_reversion = MeanReversion()\n",
    "    dollar_volume = AverageDollarVolume(window_length=30)\n",
    "    return Pipeline(columns={'longs': mean_reversion.bottom(N_LONGS),\n",
    "                             'shorts': mean_reversion.top(N_SHORTS),\n",
    "                             'ranking': mean_reversion.rank(ascending=False)},\n",
    "                    screen=dollar_volume.top(VOL_SCREEN))\n",
    "\n",
    "\n",
    "def exec_trades(data, assets, target_percent):\n",
    "    \"\"\"Place orders for assets using target portfolio percentage\"\"\"\n",
    "    for asset in assets:\n",
    "        if data.can_trade(asset) and not get_open_orders(asset):\n",
    "            order_target_percent(asset, target_percent)\n",
    "\n",
    "\n",
    "def rebalance(context, data):\n",
    "    \"\"\"Compute long, short and obsolete holdings; place trade orders\"\"\"\n",
    "    factor_data = context.factor_data\n",
    "    record(factor_data=factor_data.ranking)\n",
    "\n",
    "    assets = factor_data.index\n",
    "    record(prices=data.current(assets, 'price'))\n",
    "\n",
    "    longs = assets[factor_data.longs]\n",
    "    shorts = assets[factor_data.shorts]\n",
    "    divest = set(context.portfolio.positions.keys()) - set(longs.union(shorts))\n",
    "\n",
    "    exec_trades(data, assets=divest, target_percent=0)\n",
    "    exec_trades(data, assets=longs, target_percent=1 / N_LONGS)\n",
    "    exec_trades(data, assets=shorts, target_percent=-1 / N_SHORTS)\n",
    "\n",
    "\n",
    "def initialize(context):\n",
    "    \"\"\"Setup: register pipeline, schedule rebalancing,\n",
    "        and set trading params\"\"\"\n",
    "    attach_pipeline(compute_factors(), 'factor_pipeline')\n",
    "    schedule_function(rebalance,\n",
    "                      date_rules.week_start(),\n",
    "                      time_rules.market_open(),\n",
    "                      calendar=calendars.US_EQUITIES)\n",
    "    context.set_commission(commission.PerShare(cost=.01, min_trade_cost=0))\n",
    "    context.set_slippage(slippage.VolumeShareSlippage())\n",
    "\n",
    "\n",
    "def before_trading_start(context, data):\n",
    "    \"\"\"Run factor pipeline\"\"\"\n",
    "    context.factor_data = pipeline_output('factor_pipeline')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:ml4t-zipline]",
   "language": "python",
   "name": "conda-env-ml4t-zipline-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "512px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}